import 'package:get/get.dart';
import 'package:tin_flutter/ui/activity/brand/recommend/brand_recommend_state.dart';

import '../../../../app/network/http_service.dart';
import '../../../../app/tools/logger.dart';

/// * 作者: Tao
/// * 日期: 2025/11/10
/// * 时间: 16:38
/// * 描述: 品牌推荐logic
class BrandRecommendLogic extends GetxController {
  final state = BrandRecommendState();

  /// 分页获取品牌推荐列表
  void getBrandList({bool isRefresh = false}) async {
    if (!isRefresh && !state.hasMore.value) return;

    state.isLoading.value = true;

    if (isRefresh) {
      state.currentPage.value = 1;
    }

    try {
      final response = await HttpService.getRecommendBrandList(
        state.currentPage.value,
        6,
      );

      if (response.isNotEmpty) {
        if (isRefresh) {
          state.brandList.clear();
        }

        state.brandList.addAll(response);
        state.currentPage.value++;
        state.hasMore.value = response.length == 6; // 判断是否还有更多数据
      }
    } catch (e) {
      logger('获取品牌推荐列表失败：$e');
    } finally {
      state.isLoading.value = false;
    }
  }

  /// 下拉刷新
  void onRefresh() {
    getBrandList(isRefresh: true);
  }

  /// 上拉加载更多
  void onLoadMore() {
    getBrandList();
  }
}

